$(function(){
	$("#sendBtn").click(send_letter);
	$(".close").click(delete_msg);
});

function send_letter() {
	var targetName = $("#recipient-name").val();
	var content = $("#message-text").val();
	if (targetName.trim()==""){
		$("#recipient-name").val("");
		$("#recipient-name").attr("placeholder","用户名不能为空");
		return false;
	}
	if (content.trim()==""){
		$("#message-text").val("");
		$("#message-text").attr("placeholder","内容不能为空");
		return false;
	}
	$("#sendModal").modal("hide");
	$.post(
		CONTEXT_PATH+"/message/send",
		{
			"targetName":targetName,
			"content":content
		},
		function (data) {
			data=$.parseJSON(data);
			$("#hintBody").text(data.msg);
			$("#hintModal").modal("show");
			setTimeout(function(){
				$("#hintModal").modal("hide");
				if (data.code==0){
					location.reload();
				}
			}, 2000);
		}
	)
}

function delete_msg() {
	// TODO 删除数据
	var messageId = $(this).parents(".media").find("input").val();
	var that=this;
	$.post(
		CONTEXT_PATH+"/message/delete",
		{"letterId":messageId},
		function (data) {
			data=$.parseJSON(data);
			if (data.code==0){
				$(that).parents(".media").remove();
			}
		}
	)
}